// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <gtest/gtest.h>

#include <pollux/common/file/file_systems.h>
#include <pollux/testing/dwio/batch_maker.h>
#include <pollux/exec/row_container.h>
#include <pollux/common/file/temp_directory_path.h>
#include <pollux/serializers/compact_row_serializer.h>
#include <pollux/serializers/presto_serializer.h>
#include <pollux/serializers/unsafe_row_serializer.h>
#include <pollux/testing/vector/vector_test_base.h>

namespace kumo::pollux::exec::test {

class RowContainerTestBase : public testing::Test,
                             public pollux::VectorBuilder {
 protected:
  void SetUp() override {
    if (!isRegisteredVectorSerde()) {
      kumo::pollux::serializer::presto::PrestoVectorSerde::
          registerVectorSerde();
    }
    if (!isRegisteredVectorSerde()) {
      kumo::pollux::serializer::presto::PrestoVectorSerde::
          registerVectorSerde();
    }
    if (!isRegisteredVectorSerde()) {
      kumo::pollux::serializer::presto::PrestoVectorSerde::
          registerVectorSerde();
    }
    if (!isRegisteredVectorSerde()) {
      kumo::pollux::serializer::presto::PrestoVectorSerde::
          registerVectorSerde();
    }
    if (!isRegisteredNamedVectorSerde(VectorSerde::Kind::kPresto)) {
      kumo::pollux::serializer::presto::PrestoVectorSerde::
          registerNamedVectorSerde();
    }
    if (!isRegisteredNamedVectorSerde(VectorSerde::Kind::kCompactRow)) {
      kumo::pollux::serializer::CompactRowVectorSerde::
          registerNamedVectorSerde();
    }
    if (!isRegisteredNamedVectorSerde(VectorSerde::Kind::kUnsafeRow)) {
      kumo::pollux::serializer::spark::UnsafeRowVectorSerde::
          registerNamedVectorSerde();
    }
    filesystems::registerLocalFileSystem();
  }

  RowVectorPtr makeDataset(
      const TypePtr& rowType,
      const size_t size,
      std::function<void(RowVectorPtr)> customizeData) {
    auto batch = std::static_pointer_cast<RowVector>(
        pollux::test::BatchMaker::createBatch(rowType, size, *pool_));
    if (customizeData) {
      customizeData(batch);
    }
    return batch;
  }

  std::unique_ptr<RowContainer> makeRowContainer(
      const std::vector<TypePtr>& keyTypes,
      const std::vector<TypePtr>& dependentTypes,
      bool isJoinBuild = true) {
    auto container = std::make_unique<RowContainer>(
        keyTypes,
        !isJoinBuild,
        std::vector<Accumulator>{},
        dependentTypes,
        isJoinBuild,
        isJoinBuild,
        true,
        true,
        pool_.get());
    POLLUX_CHECK(container->testingMutable());
    return container;
  }
};
} // namespace kumo::pollux::exec::test
